package com.code.translate.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author ggh
 * @date 2022/7/20
 */
public class MD5Utils {
    private static final char[] DIGITS_LOWER = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    private static final ThreadLocal<MessageDigest> MESSAGE_DIGEST_LOCAL = new ThreadLocal<MessageDigest>() {
        @Override
        protected MessageDigest initialValue() {
            try {
                return MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException var2) {
                return null;
            }
        }
    };

    private MD5Utils() {
    }

    public static String md5Hex(byte[] bytes) throws NoSuchAlgorithmException {
        String var2;
        try {
            MessageDigest messageDigest = (MessageDigest) MESSAGE_DIGEST_LOCAL.get();
            if (messageDigest == null) {
                throw new NoSuchAlgorithmException("MessageDigest get MD5 instance error");
            }

            var2 = encodeHexString(messageDigest.digest(bytes));
        } finally {
            MESSAGE_DIGEST_LOCAL.remove();
        }

        return var2;
    }

    public static String md5Hex(String value, String encode) {
        try {
            return md5Hex(value.getBytes(encode));
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    private static String encodeHexString(byte[] bytes) {
        int l = bytes.length;
        char[] out = new char[l << 1];
        int i = 0;

        for (int var4 = 0; i < l; ++i) {
            out[var4++] = DIGITS_LOWER[(240 & bytes[i]) >>> 4];
            out[var4++] = DIGITS_LOWER[15 & bytes[i]];
        }

        return new String(out);
    }

    public static String md5FileHex(File file) {
        FileInputStream fis = null;
        try {
            MessageDigest messageDigest = MESSAGE_DIGEST_LOCAL.get();

            fis = new FileInputStream(file);
            byte[] buffer = new byte[10240];
            int length;
            while ((length = fis.read(buffer)) != -1) {
                messageDigest.update(buffer, 0, length);
            }
            return encodeHexString(messageDigest.digest());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws Exception {
//        System.out.println(encodeHexString("7654356".getBytes()));
//        System.out.println(encodeHexString("-----------".getBytes()));
//        System.out.println(md5Hex("autozh-CHSthis is doga109984457".getBytes()));
//        System.out.println(md5FileHex(new File("C:\\Users\\10419\\Desktop\\沙发.zip")));
//        System.out.println(md5FileHex(new File("E:\\var\\www\\gxlightmodel\\files\\component\\20221022\\沙发.zip")));
    }
}
